﻿

''' <summary> Classe de Clientes''' </summary>

Public Class CLSClientes

#Region "Atributos"
    Dim sql As String
    Dim objbanco As New CLSBanco
    Dim M_Cli_ID As Integer
    Dim M_Cli_CPF As String
    Dim M_Cli_RG As String
    Dim M_Cli_Nom As String
    Dim M_Cli_End As String
    Dim M_Cli_Num As Integer
    Dim M_Cli_Bairro As String
    Dim M_Cli_Tel As String
    Dim M_Cli_Cred As Boolean
    Dim LINHA As Integer
    Dim OBJDTLOCAL As New DataTable




#End Region

#Region "Gets e Seteres"

    Public Property ID() As Integer
        Get
            Return M_Cli_ID
        End Get
        Set(ByVal value As Integer)
            M_Cli_ID = value
        End Set
    End Property

    Public Property Nome() As String
        Get
            Return M_Cli_Nom
        End Get
        Set(ByVal value As String)
            M_Cli_Nom = value
        End Set
    End Property

    Public Property CPF() As String
        Get
            Return M_Cli_CPF
        End Get
        Set(ByVal value As String)
            M_Cli_CPF = value
        End Set
    End Property

    Public Property RG() As String
        Get
            Return M_Cli_RG
        End Get
        Set(ByVal value As String)
            M_Cli_RG = value
        End Set
    End Property


    Public Property Endereço() As String
        Get
            Return M_Cli_End
        End Get
        Set(ByVal value As String)
            M_Cli_End = value
        End Set
    End Property

    Public Property Numero() As Integer
        Get
            Return M_Cli_Num
        End Get
        Set(ByVal value As Integer)
            M_Cli_Num = value
        End Set
    End Property
    Public Property Bairro() As String
        Get
            Return M_Cli_Bairro
        End Get
        Set(ByVal value As String)
            M_Cli_Bairro = value
        End Set
    End Property
    Public Property Telefone() As String
        Get
            Return M_Cli_Tel
        End Get
        Set(ByVal value As String)
            M_Cli_Tel = value
        End Set
    End Property
    Public Property Crédito() As Boolean
        Get
            Return M_Cli_Cred
        End Get
        Set(ByVal value As Boolean)
            M_Cli_Cred = value
        End Set
    End Property
#End Region

#Region "Método Gravar"
    Public Sub Gravar(ByVal novo As Boolean)
        Try
            If novo Then
                sql = "insert into Cliente " & _
                  "(Cli_Nom" & _
                  ",Cli_CPF" & _
                  ",Cli_RG" & _
                  ",Cli_End" & _
                  ",Cli_Bai" & _
                  ",Cli_Num" & _
                  ",Cli_Tel" & _
                  ",Cli_Cre" & _
                  ") values " & _
                  "('" & M_Cli_Nom & "'" & _
                  ",'" & M_Cli_CPF & "'" & _
                  ",'" & M_Cli_RG & "'" & _
                  ",'" & M_Cli_End & "'" & _
                  ",'" & M_Cli_Bairro & "'" & _
                  "," & M_Cli_Num & _
                  ",'" & M_Cli_Tel & "'" & _
                  "," & M_Cli_Cred & _
                  ")"
                objbanco.executar_comando(sql)
                M_Cli_ID = buscar_ultimo_codigo()
            Else
                sql = "update Cliente set" & _
                " Cli_Nom='" & M_Cli_Nom & "'" & _
                ",Cli_CPF='" & M_Cli_CPF & "'" & _
                ",Cli_RG='" & M_Cli_RG & "'" & _
                ",Cli_End='" & M_Cli_End & "'" & _
                ",Cli_Bai='" & M_Cli_Bairro & "'" & _
                ",Cli_Tel='" & M_Cli_Tel & "'" & _
                ",Cli_Num=" & M_Cli_Num & _
                ",Cli_Cre=" & M_Cli_Cred & _
                " where Cli_ID=" & M_Cli_ID
                objbanco.executar_comando(sql)
            End If
        Catch ex As Exception
            MsgBox("Ocorreu o erro: " & ex.Message, MsgBoxStyle.Critical)
            objbanco.fechar_banco()
        End Try
    End Sub
#End Region

#Region "Método Excluir"

    Public Function excluir(ByVal meucliente As Integer) As Boolean
        Try
            sql = "delete from Cliente where Cli_ID=" & meucliente
            objbanco.executar_comando(sql)
            Return True
        Catch ex As Exception
            MsgBox("Ocorreu o erro: " & ex.Message, MsgBoxStyle.Critical)
            objbanco.fechar_banco()

        End Try
     
    End Function

#End Region

#Region "Método Localizar por Código"
    Public Function LocalizarPorID(ByVal digiteID As Integer) As DataTable
        sql = "select * from Cliente where Cli_ID = " & digiteID
        Dim objda As New OleDb.OleDbDataAdapter(sql, objbanco.objcon)
        Dim objds As New DataSet
        objda.Fill(objds)
        Return objds.Tables(0)
    End Function
#End Region

    Public Function buscar_ultimo_codigo() As Integer
        sql = "select max(Cli_Id) from cliente"
        Dim objda As New OleDb.OleDbDataAdapter(sql, objbanco.objcon)
        Dim objds As New DataSet
        objds.Tables.Clear()
        objda.Fill(objds)
        Return objds.Tables(0).Rows(0).Item(0)

    End Function

#Region "Localizar Todos Clientes"
    Public Function LocalizarTodosClientes(ByVal digiteID As String) As DataTable
        sql = "select * from Cliente"
        Dim objda As New OleDb.OleDbDataAdapter(sql, objbanco.objcon)
        Dim objds As New DataSet
        objda.Fill(objds)
        Return objds.Tables(0)
    End Function

#End Region

#Region "Método Localizar CPF"
    Public Function LocalizarPorCPF(ByVal CPF As String) As DataTable
        sql = "select * from Cliente where cLI_CPF = '" & CPF & "'"
        Dim objda As New OleDb.OleDbDataAdapter(sql, objbanco.objcon)
        Dim objds As New DataSet
        objda.Fill(objds)
        Return objds.Tables(0)
    End Function

#End Region

#Region "Método Localizar CPF duplicado"
    Public Function LocalizarPorCPFduplicado(ByVal CPF As String) As Boolean
        sql = "select * from Cliente where cLI_CPF = '" & CPF & "'"
        Dim objda As New OleDb.OleDbDataAdapter(sql, objbanco.objcon)
        Dim objds As New DataSet
        objda.Fill(objds)

        If objds.Tables(0).Rows.Count > 0 Then
            Return True
        End If

    End Function
#End Region
  
#Region "Método ir para o próximo"
    Public Sub Ir_para_proximo()
        Try
            OBJDTLOCAL = objbanco.Navegar("cliente")
            LINHA = LINHA + 1
            If LINHA > OBJDTLOCAL.Rows.Count - 1 Then
                LINHA = OBJDTLOCAL.Rows.Count - 1
            End If
            Call MOSTRAR_DADOS_NA_CLASSE()

        Catch ex As Exception
            MsgBox("Proximo registro")
        End Try
    
    End Sub
#End Region

#Region "Método ir para o ultimo"
    Public Sub Ir_para_ultimo()
        Try
            OBJDTLOCAL = objbanco.Navegar("cliente")
            LINHA = OBJDTLOCAL.Rows.Count - 1
            Call MOSTRAR_DADOS_NA_CLASSE()
        Catch ex As Exception
            MsgBox("Último registro")
        End Try
     
    End Sub
#End Region

#Region "Método ir para o primeiro"
    Public Sub Ir_para_primeiro()
        Try
            OBJDTLOCAL = objbanco.Navegar("cliente")
            LINHA = 0
            Call MOSTRAR_DADOS_NA_CLASSE()
        Catch ex As Exception
            MsgBox("Primeiro Registro")
        End Try
        
    End Sub
#End Region

#Region "Método ir para o anterior"
    Public Sub Ir_para_anterior()
        Try
            OBJDTLOCAL = objbanco.Navegar("cliente")
            LINHA = LINHA - 1
            If LINHA < 0 Then
                LINHA = 0
            End If
            Call MOSTRAR_DADOS_NA_CLASSE()
        Catch ex As Exception
            MsgBox("Registro Anterior")
        End Try
     
    End Sub
#End Region

#Region "método mostrar dados na classe"
    Private Sub MOSTRAR_DADOS_NA_CLASSE()
        ID = OBJDTLOCAL.Rows(LINHA).Item(0)
        Nome = OBJDTLOCAL.Rows(LINHA).Item(1)
        Endereço = OBJDTLOCAL.Rows(LINHA).Item(2)
        Bairro = OBJDTLOCAL.Rows(LINHA).Item(3)
        Telefone = OBJDTLOCAL.Rows(LINHA).Item(4)
        Crédito = OBJDTLOCAL.Rows(LINHA).Item(5)
        Numero = OBJDTLOCAL.Rows(LINHA).Item(6)
        CPF = OBJDTLOCAL.Rows(LINHA).Item(7)
        RG = OBJDTLOCAL.Rows(LINHA).Item(8)

    End Sub
#End Region

End Class
